<?php
class productos extends Controller {  
  
	function productos() {
		parent::Controller();
		$this->load->library("rapyd");
		//$this->datasis->modulo_id(100,1);
	}  
	function index(){
		$this->rapyd->load("datagrid2");	  
		$this->rapyd->load("dataform");
		$this->load->helper('openflash');
				
		$grid = new DataGrid2();
		
		$select=array("a.codigoa","a.desca","SUM(a.cana) AS grantotal","SUM(a.tota) AS total");
		     		
		$grid->db->select($select);  
		$grid->db->from("sitems AS a");
		$grid->db->where("devuelta",'N');
		$grid->db->groupby('a.codigoa'); 
		$grid->db->orderby('grantotal DESC');
		
		$grid->column("Producto" , "desca","align='left'");          
		$grid->column("Cantidad", "grantotal",'align=right');
		$grid->column("Monto", "total",'align=right');
		
		$grid->totalizar('total');
		$grid->build();
				
		$grafico = open_flash_chart_object(900,400, site_url("estadisticas/productos/gproductos"));
		
		$data['content'] = $grafico;
		$data['content'] .= $grid->output;

		$data["head"]    = script("jquery.pack.js").script("plugins/jquery.numeric.pack.js").script("plugins/jquery.floatnumber.js").$this->rapyd->get_head();
		$data['filtro'] =' ';
		$data['title']    = $this->rapyd->get_head()."<h1>Productos</h1>";
		$this->load->view('view_ventanas_sola', $data);
	}
	function gproductos(){
		$this->load->library('Graph');
		
		$mSQL = "SELECT a.codigoa,LEFT(a.desca,20)AS desca,SUM(a.cana) AS grantotal,SUM(a.tota) AS total
		FROM sitems AS a 
		WHERE devuelta='N'
		GROUP BY a.codigoa
		ORDER BY grantotal DESC LIMIT 10"; 
						
		//echo $mSQL;
		   	
		$maxval=0;
		$query=$this->db->query($mSQL);
		
		$data=array();
		$i=0;
		$total=0;
		foreach($query->result() as $row ){
		if ($row->grantotal>$maxval)
			$maxval=$row->grantotal;
		 	$codigoa[]=$row->codigoa;
		 	$desca[]=$row->desca;
		 	$data[]=$row->grantotal;
		 	$total+=$row->grantotal;
		}
								
		$bar_red = new bar_fade( 55, '#C31812' );
		$bar_red->key( 'Cantidad', 10 );
		
		
		//$bar_blue = new bar_fade( 55, '#424581' );
		//$bar_blue->key( 'Monto', 10 );
		  
		for($i=0;$i<count($data);$i++ ){
			$bar_red->add_data_tip($data[$i], graph::esc( number_format($data[$i],2,',','.')));
			//$bar_blue->add_data_tip($data[$i], graph::esc( number_format($data[$i],2,',','.')));
		} 
				
		$g = new graph();
		$g->title( 'Los 10 Productos mas Vendidos ', '{font-size:20px; color: #0D358A; margin: 5px; padding:5px; padding-left: 20px; padding-right: 20px;}' );
			
		$g->data_sets[] = $bar_red;               
		//$g->data_sets[] = $bar_blue;              
		                                          
		$g->x_axis_colour( '#909090', '#D2D2FB' );
		$g->y_axis_colour( '#909090', '#D2D2FB' );
		
		$g->set_x_labels($desca);
		$g->set_x_label_style(10, '#000000', 2, 1 );
		$g->set_x_axis_steps(10);
		
		//$g->set_x_legend('Total de Clientes '.number_format($total,0,'','.'), 14, '#004381' );
		$g->set_y_max($maxval);  
		$g->y_label_steps(5);
		$g->set_y_legend( 'Fyco Telecomunicaciones', 12, '#0D358A' );
		echo $g->render();
		
	}
}
?>